<?php
namespace app\api\extra;
/*
 * content : 邮件系统，版本：v1,引入第三方邮件集成
 * Author  : Songyicong 
 * time    : 2018-04-19 14:49:27 
 * email   : m13726262373@163.com 
 */
use think\Cache;

 class Email{

    /**
     * 发送登录信息
     * state:1.验证码登录，2.跳转链接登录
     */
    public static function sendLogin($send_email="",$state="1",$code_len=6,$code_state = 1,$code_min=60*10){
        if(empty($send_email) || empty($state)){
            return false;
        }
        $html = "";        
        switch($state){
            case 1:
                $html = self::getCodeHtml($send_email,$code_len,$code_state,$code_min,2);
            break;
            case 2:
                $html = self::getHref($send_email);
            break;
        }
        return sendEmail([['user_email' => $send_email, 'content' => $html]]);
    }

    /**
     * 发送用户注册信息 
     * state:1.验证码登录，2.跳转链接登录
     */
    public static function sendRegist($send_email="",$state="1",$code_len=6,$code_state = 1,$code_min=60*10){
        if(empty($send_email) || empty($state)){
            return false;
        }
        $html = "";    
        switch($state){
            case 1:
                $html = self::getCodeHtml($send_email,$code_len,$code_state,$code_min,1);
            break;
            case 2:
                $html = self::getHref($send_email);
            break;
        }
        return sendEmail([['user_email' => $send_email, 'content' => $html]]);
    }

    /** 发送跳转链接进行注册或登录 */
    public static function getHref($send_email,$state){

    }

    /** 验证码验证 ，state:1查询一次重新获取，2判断一次不用重复获取(传入更多个，返回2)*/
    public static function verifyCode($key,$code,$state=2){
        switch($state){
            case 1:
                return strtolower($code) == strtolower(Cache::pull('code_'.$key));
            default:
                $flag = strtolower($code) == strtolower(Cache::get('code_'.$key));
                if($flag){
                    Cache::pull("code_".$key);
                    return true;
                }
            return false;
        }
    }
    
    /**
     * 生成code html模板
     * key  可以是手机号或者邮件
     * code 验证码
     * len  验证码长度
     * state 1表示只有数字，2表示只有字符串，3表示字符串数字混有
     * min 代表过期时间
     * type 1注册，2登录
     */
    public static function getCodeHtml($key,$len=6,$state=1,$min=60*10,$type=1){
        $code = self::_getCode($len,$state);
        $text = "****";
        if($min>=0 && $min<60)
            $text = $min . "秒内";
        if($min<60*60&&$min>=60)
            $text = (($min/60)) . "分钟内";
        if($min>=60*60 && $min<60*60*24)
            $text = (($min/3600)) . "小时内";
        if($min >= 60*60*24 && $min < 3600*24*30)
            $text = (($min/3600)) . "天内";
        // 模板
        $html = <<<eot
        <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    <script src="main.js"></script>
</head>
<body>
    <h1>您好，您的验证码是：<span style='color:red;'>$code</span>,请在<span style='color:red;'>$text</span>填写！</h1>
</body>
</html>
eot;
        if($type==1){
            // 开始保存
            Cache::set('code_regist_'.$key,$code,$min);
        }else if($type == 2){
            Cache::set('code_login_'.$key,$code,$min);
        }
        
        return $html;
    }

    protected static function _getCode($len=6,$state=1){
        // 开始生成code,通过key
        switch($state){
            case 2:
                $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                break;
            case 3:
                $str_pol = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
                break;
            default:
                $str_pol = "1234567890012345678998765432100147852369987532146075395184260";
                break;
        }
        return substr(str_shuffle($str_pol), 0, $len);
    }
 }